import os
import random
import subprocess
import sys
from os import system
from time import sleep

total = 0

def do_touch_screnOnce(deviceid):
    startX = random.randint(200, 300)
    startY = random.randint(600, 700)

    endX = random.randint(200, 300)
    endY = random.randint(200, 400)

    print("==========================================")
    get_device_name(deviceid)
    cmd = "adb -s " + deviceid + " shell input swipe " + str(startX) + " " + str(startY) + " " + str(
        endX) + " " + str(endY) + " 200"
    # system("adb shell input swipe 100 1000 100 200 500")
    system(cmd)

    print("endX:" + str(endX) + " startX:" + str(startX))
    print("endY:" + str(endY) + " startY:" + str(startY))
    global total
    print("执行第 " + str(total) + " 次 " + cmd)

    total = total + 1


def get_all_devices():
    if os.name.startswith("window"):
        obj = subprocess.Popen('C:\\platform-tools_r29.0.5-windows\\platform-tools\\adb.exe devices', shell=True,
                               stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    else:
        obj = subprocess.Popen('adb devices', shell=True,
                               stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

    lines = obj.stdout.readlines()

    devices = []
    for line in lines:
        if line.strip() and not (line.decode('utf8').strip() == "List of devices attached"):
            devices.append(line.decode('utf8').strip().split("\t")[0])
    return devices


def get_device_name(deviceid):
    system("adb -s " + deviceid + " shell getprop ro.product.model")


if __name__ == '__main__':

    print("current os is:", sys.platform)

    while True:
        devices = get_all_devices()
        time = random.randint(5000, 9000) * 0.001
        for line in devices:
            do_touch_screnOnce(line)
        sleep(time)
        print("delay time", time)
